﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityModels;
using BusinessLayer;

namespace PresentationLayer
{
    public partial class Profile : AbstractPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NewTemplate master = (NewTemplate)Master;
            master.BodyTag.Attributes.Add("onload", "changeArrow('slideHeader_myEvents');showHideContent('slideHeader_myEvents');");

            try
            {
                User loggedInUser = GetUserFromCookie();

                if (loggedInUser != null)
                {
                    List<Event> myevents = Controller.Instance.GetEvents(loggedInUser, (byte)1);
                    Session["myevents"] = myevents;
                    List<Event> attendingevents = Controller.Instance.GetEvents(loggedInUser, (byte)2);
                    Session["attendingevents"] = attendingevents;
                    List<Event> pendingevents = Controller.Instance.GetEvents(loggedInUser, (byte)3);
                    Session["pendingevents"] = pendingevents;
                }
            }
            catch (Exception ex)
            {
                this.debugMessage.InnerHtml += ex.Message;
            }
        }
        public List<Event> GetMyEvents()
        {

            return (List<Event>)Session["myevents"];
        }
        public List<Event> GetMyAttendingEvents()
        {

            return (List<Event>)Session["attendingevents"];
        }
        public List<Event> GetMyPendingEvents()
        {
    
            return (List<Event>)Session["pendingevents"];
        }
        public void GetFinalLocation(object sender, EventArgs e)
        {
            Event ev = new Event();
            Label lb = (Label)sender;
            ev = Controller.Instance.GetEvent(Convert.ToInt32(lb.Attributes["idevent"]));
            List<Location> loc = BusinessLayer.Controller.Instance.getLocations(ev);
            
            if (loc.Count == 1) {
                lb.Text = loc[0].country + ", " + loc[0].city + ", " + loc[0].address + ", " + loc[0].locationplace;
            }
            else if (Controller.Instance.VotingFinished(ev))
            {
                Location res = Controller.Instance.GetLocationVoted(ev);
                lb.Text = res.country + ", " + res.city + ", " + res.address + ", " + res.locationplace;
            }
            else {
                lb.Text = "VOTING";
            }  
        }
    
        public void GetFinalDate(object sender, EventArgs e)
        {
            Event ev = new Event();
            Label lb = (Label)sender;
            ev = Controller.Instance.GetEvent(Convert.ToInt32(lb.Attributes["idevent"]));
            List<DateOptions> dates = Controller.Instance.getDates(ev);
            if (dates.Count == 1)
            {
                lb.Text = dates[0].DATE.ToShortDateString();
            }
            else if (Controller.Instance.VotingFinished(ev))
            {
                lb.Text = Controller.Instance.GetDateVoted(ev).ToString();
            }
            else
            {
                lb.Text = "VOTING";
            }  
        }

        public void GetFinalTime(object sender, EventArgs e)
        {
            Event ev = new Event();
            Label lb = (Label)sender;
            ev = Controller.Instance.GetEvent(Convert.ToInt32(lb.Attributes["idevent"]));
            List<TimeOptions> times = Controller.Instance.getTimes(ev);
            if (times.Count == 1)
            {
                lb.Text = times[0].TIME.ToString();
            }
            else if (Controller.Instance.VotingFinished(ev))
            {
                lb.Text = Controller.Instance.GetDateVoted(ev).ToString();
            }
            else
            {
                lb.Text = "VOTING";
            }
        }
        public void ViewEvent(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            int id = Convert.ToInt32(bt.Attributes["idevent"]);
            Response.Redirect("ViewEvent.aspx");


        }
        public void NumAttending(object sender, EventArgs e)
        {
            Event ev = new Event();
            Label lb = (Label)sender;
            ev = Controller.Instance.GetEvent(Convert.ToInt32(lb.Attributes["idevent"]));
            int num = Controller.Instance.GetUsers(ev, (byte)1).Count;
            lb.Text = num.ToString();
         }
        public void NumInvited(object sender, EventArgs e)
        {
            Event ev = new Event();
            Label lb = (Label)sender;
            ev = Controller.Instance.GetEvent(Convert.ToInt32(lb.Attributes["idevent"]));
            int num = Controller.Instance.GetUsers(ev, (byte)0).Count;
            lb.Text = num.ToString();
        }
      
       
        
    }
}